//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "FdmExtOUJumpSolver.h"
using namespace Cephei::QL::Experimental::Finitedifferences;
#include <gen/QL/Experimental/Processes/ExtOUWithJumpsProcess.h>
#include <gen/QL/Termstructures/YieldTermStructure.h>
#include <gen/QL/Methods/Finitedifferences/Solvers/FdmSolverDesc.h>
#include <gen/QL/Methods/Finitedifferences/Solvers/FdmSchemeDesc.h>
using namespace Cephei::QL::Experimental::Processes;
using namespace Cephei::QL::Termstructures;
using namespace Cephei::QL::Methods::Finitedifferences::Solvers;
#define HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Experimental::Finitedifferences::CFdmExtOUJumpSolver::CFdmExtOUJumpSolver (Cephei::QL::Experimental::Processes::IExtOUWithJumpsProcess^ process, Cephei::QL::Termstructures::IYieldTermStructure^ rTS, Cephei::QL::Methods::Finitedifferences::Solvers::IFdmSolverDesc^ solverDesc, Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Methods::Finitedifferences::Solvers::IFdmSchemeDesc^>^ schemeDesc) 
{
	_pSpinlock = new boost::detail::spinlock ();
    CExtOUWithJumpsProcess^ _Cprocess;
    CYieldTermStructure^ _CrTS;
    CFdmSolverDesc^ _CsolverDesc;
    CFdmSchemeDesc^ _CschemeDesc;
    try
    {
#ifdef HANDLE
        _phFdmExtOUJumpSolver = NULL;
#endif
        _Cprocess = safe_cast<CExtOUWithJumpsProcess^> (process);
        _Cprocess->Lock();
        Handle<QuantLib::ExtOUWithJumpsProcess>& _process = static_cast<Handle<QuantLib::ExtOUWithJumpsProcess>&> (_Cprocess->GetHandle ()); 
        _CrTS = safe_cast<CYieldTermStructure^> (rTS);
        _CrTS->Lock();
        boost::shared_ptr<QuantLib::YieldTermStructure>& _rTS = static_cast<boost::shared_ptr<QuantLib::YieldTermStructure>&> (_CrTS->GetShared ()); 
        _CsolverDesc = safe_cast<CFdmSolverDesc^> (solverDesc);
        _CsolverDesc->Lock();
        QuantLib::FdmSolverDesc& _solverDesc = static_cast<QuantLib::FdmSolverDesc&> (_CsolverDesc->GetReference ()); 
        if (Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Methods::Finitedifferences::Solvers::IFdmSchemeDesc^>::IsSome::get (schemeDesc))
        {
            _CschemeDesc = safe_cast<CFdmSchemeDesc^> (schemeDesc->Value);
            _CschemeDesc->Lock();
        }
        QuantLib::FdmSchemeDesc& _schemeDesc = 
            (Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Methods::Finitedifferences::Solvers::IFdmSchemeDesc^>::IsSome::get (schemeDesc) ? static_cast<QuantLib::FdmSchemeDesc&> (_CschemeDesc->GetReference ()) : QuantLib::FdmSchemeDesc::Hundsdorfer()); //1
        _ppFdmExtOUJumpSolver = new boost::shared_ptr<QuantLib::FdmExtOUJumpSolver> (new QuantLib::FdmExtOUJumpSolver ( _process,  _rTS,  _solverDesc,  _schemeDesc ));
        
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cprocess != nullptr) _Cprocess->Unlock();
        if (_CrTS != nullptr) _CrTS->Unlock();
        if (_CsolverDesc != nullptr) _CsolverDesc->Unlock();
        if (_CschemeDesc != nullptr) _CschemeDesc->Unlock();
    }
}
Cephei::QL::Experimental::Finitedifferences::CFdmExtOUJumpSolver::CFdmExtOUJumpSolver (boost::shared_ptr<QuantLib::FdmExtOUJumpSolver>& childNative, Object^ owner) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phFdmExtOUJumpSolver = NULL;
#endif
	_ppFdmExtOUJumpSolver = &childNative;
    
}
Cephei::QL::Experimental::Finitedifferences::CFdmExtOUJumpSolver::CFdmExtOUJumpSolver (QuantLib::FdmExtOUJumpSolver& childNative, Object^ owner) 
{
#ifdef HANDLE
	_phFdmExtOUJumpSolver = NULL;
#endif
	_ppFdmExtOUJumpSolver = new boost::shared_ptr<QuantLib::FdmExtOUJumpSolver> (&childNative);
    
    _FdmExtOUJumpSolverOwner = owner;
    
}

Cephei::QL::Experimental::Finitedifferences::CFdmExtOUJumpSolver::CFdmExtOUJumpSolver (CFdmExtOUJumpSolver^ copy) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phFdmExtOUJumpSolver = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppFdmExtOUJumpSolver = new boost::shared_ptr<QuantLib::FdmExtOUJumpSolver> (copy->GetShared());
        
    }
}
Cephei::QL::Experimental::Finitedifferences::CFdmExtOUJumpSolver::CFdmExtOUJumpSolver (PLATFORM::Type^ t) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phFdmExtOUJumpSolver = NULL;
#endif
	if (!t->IsSubclassOf(CFdmExtOUJumpSolver::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Experimental::Finitedifferences::CFdmExtOUJumpSolver::CFdmExtOUJumpSolver (QuantLib::Handle<QuantLib::FdmExtOUJumpSolver>& childNative, Object^ owner)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phFdmExtOUJumpSolver = &childNative;
	_ppFdmExtOUJumpSolver = &static_cast<boost::shared_ptr<QuantLib::FdmExtOUJumpSolver>>(childNative.currentLink());
    
    _FdmExtOUJumpSolverOwner = owner;
}
Cephei::QL::Experimental::Finitedifferences::CFdmExtOUJumpSolver::CFdmExtOUJumpSolver (QuantLib::Handle<QuantLib::FdmExtOUJumpSolver> childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phFdmExtOUJumpSolver = &childNative;
	_ppFdmExtOUJumpSolver = &static_cast<boost::shared_ptr<QuantLib::FdmExtOUJumpSolver>>(childNative.currentLink());
    
}
#endif
#ifdef STRUCT
Cephei::QL::Experimental::Finitedifferences::CFdmExtOUJumpSolver::CFdmExtOUJumpSolver (QuantLib::FdmExtOUJumpSolver childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phFdmExtOUJumpSolver = NULL;
#endif
	_ppFdmExtOUJumpSolver = new boost::shared_ptr<QuantLib::FdmExtOUJumpSolver> (new QuantLib::FdmExtOUJumpSolver (childNative));
    
}
#endif

Cephei::QL::Experimental::Finitedifferences::CFdmExtOUJumpSolver::~CFdmExtOUJumpSolver ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
        _pSpinlock = NULL;
    }
    if (_ppFdmExtOUJumpSolver != NULL)
    {
	    delete _ppFdmExtOUJumpSolver;
        _ppFdmExtOUJumpSolver = NULL;
    }
}
Cephei::QL::Experimental::Finitedifferences::CFdmExtOUJumpSolver::!CFdmExtOUJumpSolver ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
    }
    if (_ppFdmExtOUJumpSolver != NULL)
    {
	    delete _ppFdmExtOUJumpSolver;
    }
}
QuantLib::FdmExtOUJumpSolver& Cephei::QL::Experimental::Finitedifferences::CFdmExtOUJumpSolver::GetReference ()
{
    if (_ppFdmExtOUJumpSolver == NULL) throw REFNEW NativeNullException ();
	return **_ppFdmExtOUJumpSolver;
}
boost::shared_ptr<QuantLib::FdmExtOUJumpSolver>& Cephei::QL::Experimental::Finitedifferences::CFdmExtOUJumpSolver::GetShared ()
{
    if (_ppFdmExtOUJumpSolver == NULL) throw REFNEW NativeNullException ();
	return *_ppFdmExtOUJumpSolver;
}
QuantLib::FdmExtOUJumpSolver* Cephei::QL::Experimental::Finitedifferences::CFdmExtOUJumpSolver::GetPointer ()
{
    if (_ppFdmExtOUJumpSolver == NULL) throw REFNEW NativeNullException ();
	return &**_ppFdmExtOUJumpSolver;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::FdmExtOUJumpSolver>& Cephei::QL::Experimental::Finitedifferences::CFdmExtOUJumpSolver::GetHandle ()
{
	if (_phFdmExtOUJumpSolver == NULL)
	{
		_phFdmExtOUJumpSolver = new Handle<QuantLib::FdmExtOUJumpSolver> (*_ppFdmExtOUJumpSolver);
	}
	return *_phFdmExtOUJumpSolver;
}
#endif
bool Cephei::QL::Experimental::Finitedifferences::CFdmExtOUJumpSolver::HasNative () 
{
	return (_ppFdmExtOUJumpSolver != NULL);
}

Double Cephei::QL::Experimental::Finitedifferences::CFdmExtOUJumpSolver::ValueAt (Double x, Double y)
{
    try
    {
        QuantLib::Real _x = (QuantLib::Real)ValueHelper::Convert (x); //a
        QuantLib::Real _y = (QuantLib::Real)ValueHelper::Convert (y); //a
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppFdmExtOUJumpSolver)->valueAt ( _x,  _y );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Experimental::Finitedifferences::IFdmExtOUJumpSolver^ Cephei::QL::Experimental::Finitedifferences::CFdmExtOUJumpSolver_Factory::Create (Cephei::QL::Experimental::Processes::IExtOUWithJumpsProcess^ process, Cephei::QL::Termstructures::IYieldTermStructure^ rTS, Cephei::QL::Methods::Finitedifferences::Solvers::IFdmSolverDesc^ solverDesc, Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Methods::Finitedifferences::Solvers::IFdmSchemeDesc^>^ schemeDesc)
{
    return REFNEW CFdmExtOUJumpSolver ( process,  rTS,  solverDesc,  schemeDesc);
}
